JavaScript Interview Questions and Answers
JavaScript vs Jquery | JavaScript vs VBScript | JavaScript vs TypeScript |
Ques 21. What are Math routines in Javascript?
the Math object contains functions and constants
Math.sqrt
Math.pow
Math.abs
Math.max
Math.min
Math.floor
Math.ceil
Math.round
Math.PI
Math.E
Math.random function returns number in [0..1)
Ques 22. Write a javascript to get difference between two dates.
now = new Date();
newYear = new Date(2004,0,1);
secs = Math.round((now-newYear)/1000);
days = Math.floor(secs / 86400);
secs = days*86400;
hours = Math.floor(secs / 3600);
secs = hours*3600;
minutes = Math.floor(secs / 60);
secs = minutes*60
document.write(days + " days, " +
hours + " hours, " +
minutes + " minutes, and " +
secs + " seconds.");
Ques 23. What about Navigator object?
navigator.appName property that gives the browser name.
navigator.appVersion property that gives the browser version.
navigator.userAgent property that gives the browser related information.
Ques 24. What is User-Defined class in Javascript?
can define new classes, but the notation is awkward
simply define a function that serves as a constructor
specify data fields & methods using this
no data hiding: can't protect data or methods
function Die(sides)
{
this.numSides = sides;
this.numRolls = 0;
this.Roll = Roll;
}
function Roll()
{
this.numRolls++;
return Math.floor(Math.random()*this.numSides) + 1;
}
die6 = new Die(6);
die8 = new Die(8);
roll6 = -1; // dummy value to start loop
roll8 = -2; // dummy value to start loop
while (roll6 != roll8) {
roll6 = die6.Roll();
roll8 = die8.Roll();
document.write("6-sided: " + roll6 +
" " +
"8-sided: " + roll8 + "
");
}
Ques 25. Few events in Javascript.
onsubmit - call when submit button is clicked
onclick - call when this button is clicked
onreset - call when the reset button is clicked
onload - call after page loads
onmouseover - call when mouse pointer enters image area
onmouseout - call when mouse pointer leaves image area
onfocus - call when control receives focus
onblur - call when a control loses focus
onchange - call when a control loses focus and the value of its contents has changed
onunload – call when a page is closed